package cn.wolfcode.luowowo.website.advice;

import cn.wolfcode.luowowo.common.exception.logicException;
import cn.wolfcode.luowowo.member.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ControllerAdvice 给controller增强功能
 *  因为每一个controller都要try catch，重复很多遍，跟事务很像
 *  所以用aop得方法统一异常处理
 *  对请求方法前和请求方法后动态增强功能
 */
@ControllerAdvice
public class CommonExceptionHandler {

    //异常处理标签，里面得参数表示要处理得异常
    @ExceptionHandler({logicException.class})
    @ResponseBody
    public AjaxResult displayExp(Exception e) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false,e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public AjaxResult runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false, "系统出错啦，请联系管理员。");
    }
}
